package com.axun.ckz.mysocketdemo.base

import android.app.AlertDialog
import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast

import com.axun.ckz.mysocketdemo.R


/**
 * Created by ckz on 2018/5/3.
 */

open class BaseFragment : Fragment(), IBaseView {
    private var dialog: AlertDialog? = null
    private var dialogView: View? = null
    private var tvMsg: TextView? = null
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        return super.onCreateView(inflater, container, savedInstanceState)
    }

    fun showToast(toast: String) {
        Toast.makeText(context, toast, Toast.LENGTH_SHORT).show()
    }

    override fun showProgress(progres: String) {
        dialog = AlertDialog.Builder(context, R.style.LoadDialog).create()
        dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_progress, null)
        tvMsg = dialogView!!.findViewById(R.id.tv_msg)
        dialog!!.show()
        dialog!!.setContentView(dialogView!!)
        if (!TextUtils.isEmpty(progres)) {
            tvMsg!!.visibility = View.VISIBLE
            tvMsg!!.text = progres
        } else {
            tvMsg!!.visibility = View.GONE
        }
    }

    override fun hideProgress() {
        if (dialog != null && dialog!!.isShowing) {
            dialog!!.dismiss()
        }
    }

    override fun showUI(json: String) {

    }

    override fun showError(error: String) {
        showToast(error)
    }

    override fun showFailed(msg: String) {
        showToast(msg)
    }
}
